---
title: "Policy Index"
output: html_document
---

Run the following to read in the file and send it out to the coders for them to fill in their information. 

First, read the file either from computer or Google Drive. Comment out lines not used.
Then, add seven new rows for a week.
Last, reupload to Google Drive for coders to fill out.

```{r}
library(haven)
library(tidyverse)
library(dplyr)
library(RStata)
library(googledrive)
library(readr)
library(zoo)

#SENDING OUT TO CODERS
#read in masterfile from computer. Change path and name as needed.
masterFile <- read_dta("COVID_Brazil_May14Update1.dta")

#OR

#read in masterfile from drive.
# drive_download(file = "CovidTimeline", path = "CovidTimeline.csv", overwrite = TRUE)
# masterFile <- read.csv("CovidTimeline.csv")

masterFile$date <- as.Date(masterFile$date, format = "%m/%d/%Y")
masterFile$Date <- as.Date(masterFile$Date)

#automatically add seven rows to each state
stateNumRows = length(which(masterFile$State_code == 1))
numRow = stateNumRows

for (i in unique(masterFile$State_code)) {
  emptyRows = data.frame(matrix(ncol = ncol(masterFile), nrow = 7))
  names(emptyRows) <- names(masterFile)
  emptyRows$Country = 1
  emptyRows$State_code = i
  emptyRows$State_Name = i
  emptyRows$date = c(masterFile$date[numRow] + 1, masterFile$date[numRow] + 2,
                     masterFile$date[numRow] + 3, masterFile$date[numRow] + 4,
                     masterFile$date[numRow] + 5, masterFile$date[numRow] + 6,
                     masterFile$date[numRow] + 7) 
  emptyRows$Date = c(masterFile$Date[numRow] + 1, masterFile$Date[numRow] + 2,
                     masterFile$Date[numRow] + 3, masterFile$Date[numRow] + 4,
                     masterFile$Date[numRow] + 5, masterFile$Date[numRow] + 6,
                     masterFile$Date[numRow] + 7) 
  emptyRows$Days = (masterFile$Days[numRow] + 1) : (masterFile$Days[numRow] + 7)
  emptyRows$population = masterFile$population[numRow]
  emptyRows$GDP_per_capita = masterFile$GDP_per_capita[numRow]
  emptyRows$GDP_Rank = masterFile$GDP_Rank[numRow]
  emptyRows$Marginalization = masterFile$Marginalization[numRow]
  masterFile <- rbind(masterFile[1:numRow,], emptyRows, masterFile[-(1:numRow),])
  numRow = numRow + 7 + stateNumRows
}

write_csv(masterFile, path = "CovidTimeline.csv")

#distribute it to the coders. wait for them to input their information
drive_put("CovidTimeline.csv", path = "~/CovidTimeline", type = "spreadsheet")

```


Run the following to update the indices and reupload to Google Drive.

First, grab the file from Google Drive.
Then, update the indices.
Then, reupload to Google Drive.

```{r}
#UPDATING INDICES
#retrieve the file
drive_download(file = "CovidTimeline", path = "CovidTimeline.csv", overwrite = TRUE)
masterFile <- read.csv("CovidTimeline.csv")

masterFile$date <- as.Date(masterFile$date)
masterFile$Date <- as.Date(masterFile$Date)

#recalculate indices
rowsToIndex = as.numeric(rownames(masterFile[is.na(masterFile$Oxford_Index),]))

for (i in rowsToIndex) {

  #Policy Index
  masterFile$Policy_Index[i] = ((masterFile$School_Closure[i] + masterFile$Workplace_Closure[i] 
                        + masterFile$Public_Events_Cancelled [i] 
                        + masterFile$Public_Transit_Suspended[i] 
                        + masterFile$Information_Campaign[i] 
                        + masterFile$Internal_Travel_Control[i] 
                        + masterFile$International_Travel_Controls[i]
		       + masterFile$Rest_on_gatherings[i]
		       + masterFile$Stay_at_home[i]) /9) * 100

#Mask Index
masterFile$Mask_Index[I] =(masterFile$Use_face_masks[I])* 100
  
  #Policy Index Adjusted Time
  indice_cierre_de_escuelas = masterFile$School_Closure[i] * 
    (masterFile$Days_Since_Schools_Closed[i]/masterFile$Days[i]) * 100
  indice_suspencion_laboral = masterFile$Workplace_Closure[i] * 
    (masterFile$Days_Workplace_Closure[i]/masterFile$Days[i]) * 100
  indice_cancelacion_eventos_pub = masterFile$Public_Events_Cancelled[i] *
    (masterFile$Public_Events_Cancelled_Days[i]/masterFile$Days[i]) * 100
  indice_suspension_transporte_pub = masterFile$Public_Transit_Suspended[i] *
    (masterFile$Days_Since_Transit_Suspend[i]/masterFile$Days[i]) * 100
  indice_campanas_informativas = masterFile$Information_Campaign[i] *
    (masterFile$Information_Campaign_Days[i]/masterFile$Days[i]) * 100
  indice_restriccion_viajes_estado = masterFile$Internal_Travel_Control[i] *
    (masterFile$Days_Since_Internal_Travel_Ban[i]/masterFile$Days[i]) * 100
  indice_control_viajes_interna = masterFile$International_Travel_Controls[i] *
    (masterFile$Days_Since_International_Ban[i]/masterFile$Days[i]) * 100
  indice_restrictedgatherings = masterFile$Rest_on_gatherings[i]*
    (masterFile$Days_rest_on_gatherings[i]/masterFile$Days[i])* 100
  indice_stayhome = masterFile$Stay_at_home[i]*
    (masterFile$Days_since_stay_at_home[i]/masterFile$Days[i])*100
  
  masterFile$Policy_Index_Adjusted_Time[i] = (indice_cierre_de_escuelas 
                                     + indice_suspencion_laboral
                                     + indice_cancelacion_eventos_pub
                                     + indice_suspension_transporte_pub
                                     + indice_campanas_informativas
                                     + indice_restriccion_viajes_estado
                                     + indice_control_viajes_interna
				   + indice_restrictedgatherings
				   + indice_stayhome) / 9
  
 

write_csv(masterFile, path = "CovidTimeline.csv")

#reupload new masterFile to drive
drive_put("CovidTimeline.csv", path = "~/CovidTimeline", type = "spreadsheet")


```


